package ru.niir.goodfil.ui;

import org.apache.log4j.Logger;
import ru.niir.goodfil.db.Order;
import ru.niir.goodfil.utils.SumFormat;

import java.util.ArrayList;
import java.util.List;

public class StorekeeperReservesBean extends PageBean {

    private static Logger logger = Logger.getLogger(OrdersBean.class);

    private static final String STOREKEEPER_PAGE = "sk2";

    private static final String STOREKEEPER_BEAN_NAME = "StorekeeperOrderBean";

    private static final int SORT_DEFAULT_MAX = 500;

    int days = gf().getIntOrCreateIfNotExists("storekeeping.days", 3, "Количество дней, за которое будут выводиться заказы на странице кладовщика");

    /**
     * Резервы
     */
    private List<Order> reservedOrders;

    public List<Order> getReservedOrders(){
        StorekeeperOrderBean bean = (StorekeeperOrderBean) getBean(STOREKEEPER_BEAN_NAME);
        bean.release();
        if (this.reservedOrders == null) {
            this.reservedOrders = new ArrayList<Order>();
            List<Order> orders = gf().getReservedOrder();
            for (Order order : orders) {
                order.setClient(gf().getClientById(order.getClientId()));
                order.setManager(gf().getUserById(order.getManagerId()));
                gf().addOrderInfo(order);
                this.reservedOrders.add(order);
            }
        }
        return this.reservedOrders;
    }

    public String getReservedOrdersSum(){
        double sum = 0;
        for(Order order : reservedOrders){
            sum += order.getSum();
        }
        return SumFormat.formatSum("###,###,##0.00", sum);
    }

    public void refresh(){
        this.reservedOrders = null;
    }
}
